﻿using System;

class WeAllLoveBits
{
    static int CalcPNew(int p)
    {
        int p1 = 0;
        int p2 = 0;
        int i = 0;
        while (true)
        {
            if (((p >> i) & 1) != 1)
            {
                int temp = p1 | (1 << i);
                if (temp > p)
                {
                    break;
                }
                else
                {
                    p1 = temp;
                }
            }
            i++;
        }
        i--;
        for (int j = i; j > -1; j--)
        {
            if (((p >> j) & 1) == 1)
            {   
                p2 = p2 | (1 << i - j);
            }
        }
        return (p ^ p1) & p2;
    }
    static void Main()
    {
        string input = Console.ReadLine();
        int n = int.Parse(input);
        for (int i = 0; i < n; i++)
        {
            input = Console.ReadLine();
            Console.WriteLine(CalcPNew(int.Parse(input)));
        }
    }
}